//Background
var bg : Texture2D;
private var screenNum: int = 1;

//Title texture
private var mainTitleTexture:Texture2D;
private var characterselectTexture:Texture2D;
private var levelselectTexture:Texture2D;

//Textures of the characters
var meleeTexture : Texture2D;
var rangeTexture : Texture2D;
var spellTexture : Texture2D;
var healerTexture : Texture2D;
var scoutTexture : Texture2D;

//Placeholders for player to select which characters to choose
private var slot1Texture:Texture2D;
private var slot2Texture:Texture2D;
private var slot3Texture:Texture2D;
private var slot4Texture:Texture2D;
private var slot5Texture:Texture2D;
private var slot6Texture:Texture2D;

//The cooresponding ID to the slot texture
//The main class will extract these ID to create the neccessary character
static var slot1ID: int = CharacterManager.PLAYER_MELEE;
static var slot2ID: int = CharacterManager.PLAYER_RANGE;
static var slot3ID: int = CharacterManager.PLAYER_SPELL;
static var slot4ID: int = CharacterManager.PLAYER_SCOUT;
static var slot5ID: int = CharacterManager.PLAYER_HEALER;
static var slot6ID: int = CharacterManager.PLAYER_HEALER;

//Map ID of the map the player has chosen
static var mapID: int = 1;

//Texture Aspect ratio
private var textureRatio:float = 1.0;

//Start button texture	
var buttonStartTexture:Texture2D;
var buttonStartHoverTexture:Texture2D;
var buttonStartPressedTexture:Texture2D;
//End button texture
var buttonEndTexture:Texture2D;
var buttonEndHoverTexture:Texture2D;
var buttonEndPressedTexture:Texture2D;

//Map selection Texture
var selectMap1:Texture2D;
var selectMap1Hover:Texture2D;
var selectMap2:Texture2D;
var selectMap2Hover:Texture2D;

//Sound
private var buttonSound:AudioClip;
private var selectSound:AudioClip;

//Styles
private var startstyle:GUIStyle = new GUIStyle();
private var backstyle:GUIStyle = new GUIStyle();
private var selectlevel1style:GUIStyle = new GUIStyle();
private var selectlevel2style:GUIStyle = new GUIStyle();

function Start ()
{
	textureRatio = (Screen.width*1.0/1500);
	screenNum = 1;

	//Refresh the ID
	slot1ID = CharacterManager.PLAYER_MELEE;
	slot2ID = CharacterManager.PLAYER_RANGE;
	slot3ID = CharacterManager.PLAYER_SPELL;
	slot4ID = CharacterManager.PLAYER_SCOUT;
	slot5ID = CharacterManager.PLAYER_HEALER;
	slot6ID = CharacterManager.PLAYER_HEALER;

	slot1Texture = meleeTexture;
	slot2Texture = rangeTexture;
	slot3Texture = spellTexture;
	slot4Texture = scoutTexture;
	slot5Texture = healerTexture;
	slot6Texture = healerTexture;
	
	mainTitleTexture = Resources.Load("Menu/title");
	characterselectTexture = Resources.Load("Menu/characterselect");
	levelselectTexture = Resources.Load("Menu/levelselect");
	
	//sound
	selectSound = Resources.Load("selectSound");
	buttonSound = Resources.Load("buttonSound");
	
	//Styles
	startstyle.normal.background = buttonStartTexture;
	startstyle.hover.background = buttonStartHoverTexture;
	startstyle.active.background = buttonStartPressedTexture;	
	
	backstyle.normal.background = buttonEndTexture;
	backstyle.hover.background = buttonEndHoverTexture;
	backstyle.active.background = buttonEndPressedTexture;		
	
	selectlevel1style.normal.background = selectMap1;
	selectlevel1style.hover.background = selectMap1Hover;
	selectlevel2style.normal.background = selectMap2;
	selectlevel2style.hover.background = selectMap2Hover;
}

function OnGUI ()
{
	GUI.depth = 1;

	//Background
	GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), bg, ScaleMode.ScaleAndCrop);
	
	var titleWidth:float = textureRatio * mainTitleTexture.width;
	GUI.DrawTexture(Rect(Screen.width-titleWidth, 0,titleWidth,mainTitleTexture.height*textureRatio), mainTitleTexture, ScaleMode.ScaleToFit);
	
	if( screenNum == 2 )
	{
		var buttonwidth:float = textureRatio * meleeTexture.width / 1.5f; 
		var buttonheight:float = textureRatio * meleeTexture.height / 1.5f;
	
		titleWidth = textureRatio * characterselectTexture.width*0.5;
		GUI.DrawTexture(Rect(0,0, titleWidth,textureRatio*characterselectTexture.height*0.5), characterselectTexture, ScaleMode.ScaleToFit);
	
		// Character ONE =========================
		if (GUI.Button(Rect(0, Screen.height/2-buttonheight, buttonwidth, buttonheight),slot1Texture,GUIStyle.none))
		{
			audio.PlayOneShot(selectSound);
		
			slot1ID += 1;
			if( slot1ID > CharacterManager.PLAYER_SCOUT )
				slot1ID = CharacterManager.PLAYER_MELEE;
			//else if( slot1ID < CharacterManager.PLAYER_MELEE )s
			//	slot1ID = CharacterManager.PLAYER_HEALER;
			
			if( slot1ID == CharacterManager.PLAYER_MELEE )
				slot1Texture = meleeTexture;
			else if( slot1ID == CharacterManager.PLAYER_RANGE )
				slot1Texture = rangeTexture;
			else if( slot1ID == CharacterManager.PLAYER_SPELL )
				slot1Texture = spellTexture;
			else if( slot1ID == CharacterManager.PLAYER_HEALER )
				slot1Texture = healerTexture;	
			else if( slot1ID == CharacterManager.PLAYER_SCOUT )
				slot1Texture = scoutTexture;		
		}
		// End of Character ONE ==================
		
		// Character TWO =========================
		if (GUI.Button( Rect(buttonwidth, Screen.height/2-buttonheight, buttonwidth, buttonheight), slot2Texture, GUIStyle.none)) 
		{
			audio.PlayOneShot(selectSound);	
		
			slot2ID += 1;
			if( slot2ID > CharacterManager.PLAYER_SCOUT )
				slot2ID = CharacterManager.PLAYER_MELEE;
			//else if( slot1ID < CharacterManager.PLAYER_MELEE )
			//	slot1ID = CharacterManager.PLAYER_HEALER;
			
			if( slot2ID == CharacterManager.PLAYER_MELEE )
				slot2Texture = meleeTexture;
			else if( slot2ID == CharacterManager.PLAYER_RANGE )
				slot2Texture = rangeTexture;
			else if( slot2ID == CharacterManager.PLAYER_SPELL )
				slot2Texture = spellTexture;
			else if( slot2ID == CharacterManager.PLAYER_HEALER )
				slot2Texture = healerTexture;	
			else if( slot2ID == CharacterManager.PLAYER_SCOUT )
				slot2Texture = scoutTexture;						
		}
		// End of Character TWO ==================
		
		// Character THREE ========================= //Screen.width/4*2+(buttonwidth/4)
		if (GUI.Button(Rect(buttonwidth*2, Screen.height/2-buttonheight, buttonwidth, buttonheight),slot3Texture,GUIStyle.none))
		{
			audio.PlayOneShot(selectSound);	
		
			slot3ID += 1;
			if( slot3ID > CharacterManager.PLAYER_SCOUT )
				slot3ID = CharacterManager.PLAYER_MELEE;
			//else if( slot1ID < CharacterManager.PLAYER_MELEE )s
			//	slot1ID = CharacterManager.PLAYER_HEALER;
			
			if( slot3ID == CharacterManager.PLAYER_MELEE )
				slot3Texture = meleeTexture;
			else if( slot3ID == CharacterManager.PLAYER_RANGE )
				slot3Texture = rangeTexture;
			else if( slot3ID == CharacterManager.PLAYER_SPELL )
				slot3Texture = spellTexture;
			else if( slot3ID == CharacterManager.PLAYER_HEALER )
				slot3Texture = healerTexture;	
			else if( slot3ID == CharacterManager.PLAYER_SCOUT )
				slot3Texture = scoutTexture;						
		}
		// End of Character THREE ==================
		
		// Character FOUR =========================
		if (GUI.Button( Rect(0, Screen.height/2, buttonwidth, buttonheight), slot4Texture, GUIStyle.none)) 
		{
			audio.PlayOneShot(selectSound);	
		
			slot4ID += 1;
			if( slot4ID > CharacterManager.PLAYER_SCOUT )
				slot4ID = CharacterManager.PLAYER_MELEE;
			//else if( slot1ID < CharacterManager.PLAYER_MELEE )
			//	slot1ID = CharacterManager.PLAYER_HEALER;
			
			if( slot4ID == CharacterManager.PLAYER_MELEE )
				slot4Texture = meleeTexture;
			else if( slot4ID == CharacterManager.PLAYER_RANGE )
				slot4Texture = rangeTexture;
			else if( slot4ID == CharacterManager.PLAYER_SPELL )
				slot4Texture = spellTexture;
			else if( slot4ID == CharacterManager.PLAYER_HEALER )
				slot4Texture = healerTexture;		
			else if( slot4ID == CharacterManager.PLAYER_SCOUT )
				slot4Texture = scoutTexture;	
		}
		// End of Character FOUR ==================
		
		// Character FIVE =========================
		if (GUI.Button(Rect(buttonwidth, Screen.height/2, buttonwidth, buttonheight),slot5Texture,GUIStyle.none))
		{
			audio.PlayOneShot(selectSound);	
		
			slot5ID += 1;
			if( slot5ID > CharacterManager.PLAYER_SCOUT )
				slot5ID = CharacterManager.PLAYER_MELEE;
			//else if( slot1ID < CharacterManager.PLAYER_MELEE )s
			//	slot1ID = CharacterManager.PLAYER_HEALER;
			
			if( slot5ID == CharacterManager.PLAYER_MELEE )
				slot5Texture = meleeTexture;
			else if( slot5ID == CharacterManager.PLAYER_RANGE )
				slot5Texture = rangeTexture;
			else if( slot5ID == CharacterManager.PLAYER_SPELL )
				slot5Texture = spellTexture;
			else if( slot5ID == CharacterManager.PLAYER_HEALER )
				slot5Texture = healerTexture;		
			else if( slot5ID == CharacterManager.PLAYER_SCOUT )
				slot5Texture = scoutTexture;					
		}
		// End of Character FIVE ==================
		
		// Character SIX =========================
		if (GUI.Button( Rect(buttonwidth*2, Screen.height/2, buttonwidth, buttonheight), slot6Texture, GUIStyle.none)) 
		{
			audio.PlayOneShot(selectSound);	
		
			slot6ID += 1;
			if( slot6ID > CharacterManager.PLAYER_SCOUT )
				slot6ID = CharacterManager.PLAYER_MELEE;
			//else if( slot1ID < CharacterManager.PLAYER_MELEE )
			//	slot1ID = CharacterManager.PLAYER_HEALER;
			
			if( slot6ID == CharacterManager.PLAYER_MELEE )
				slot6Texture = meleeTexture;
			else if( slot6ID == CharacterManager.PLAYER_RANGE )
				slot6Texture = rangeTexture;
			else if( slot6ID == CharacterManager.PLAYER_SPELL )
				slot6Texture = spellTexture;
			else if( slot6ID == CharacterManager.PLAYER_HEALER )
				slot6Texture = healerTexture;			
			else if( slot6ID == CharacterManager.PLAYER_SCOUT )
				slot6Texture = scoutTexture;			
		}
		// End of Character SIX ==================
		
		buttonwidth = textureRatio * buttonStartTexture.width; 
		buttonheight = textureRatio * buttonStartTexture.height;
		
		//Screen.width/2-(106*textureRatio), Screen.height/2+150, 212*textureRatio, 85*textureRatio
		if (GUI.Button(Rect(Screen.width-100-buttonwidth, Screen.height-buttonheight*3, buttonwidth, buttonheight),null,startstyle))
		{
			audio.PlayOneShot(buttonSound);
			Application.LoadLevel(1);
		}
		
		if (GUI.Button(Rect(Screen.width-100-buttonwidth, Screen.height-buttonheight*2, buttonwidth, buttonheight),null,backstyle))
		{
			screenNum = 1;
		}		
	}
	//Level Select
	else if( screenNum == 1 )
	{
		titleWidth = textureRatio * levelselectTexture.width*0.5;
		GUI.DrawTexture(Rect(0,0, titleWidth,textureRatio*levelselectTexture.height*0.5), levelselectTexture, ScaleMode.ScaleToFit);
		
		buttonwidth = textureRatio * selectMap1.width/1.2; 
		buttonheight = textureRatio * selectMap1.height/1.2;
		
		if (GUI.Button(Rect(Screen.width/2,Screen.height/2-buttonheight/3,buttonwidth,buttonheight),null,selectlevel1style))
		{
			screenNum = 2;
			mapID = 1;
		}		
		if (GUI.Button(Rect(Screen.width/2-buttonwidth,Screen.height/2-buttonheight/3,buttonwidth,buttonheight),null,selectlevel2style))
		{
			screenNum = 2;
			mapID = 2;
		}		
	}
}